package com.simplegovern.authority.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.simplegovern.authority.SysUserRole;
import com.simplegovern.authority.dao.UserRoleDAO;

public class UserRoleService {
	
	@Autowired
	private UserRoleDAO userRoleDAO;

	public List<Long> getRolesByUserId(Long userId){
		List<SysUserRole> surs = userRoleDAO.getUserRoleByUserId(userId);
		List<Long> list = new ArrayList<Long>();
		for (SysUserRole sur : surs) {
			list.add(sur.getRoleId());
		}
		return list;
	}
	
	public void remove(Long userId){
		List<SysUserRole> list = userRoleDAO.getUserRoleByUserId(userId);
		for (SysUserRole sysUserRole : list) {
			userRoleDAO.delete(sysUserRole.getId());
		}
	}
	
	public void add(Long[] roleIds, Long userId){
		for (Long roleId : roleIds) {
			SysUserRole sur = new SysUserRole();
			sur.setUserId(userId);
			sur.setRoleId(roleId);
			userRoleDAO.save(sur);
		}
	}
}
